package  core.social
{
import com.adobe.serialization.json.JSON;

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.utils.Dictionary;

import mx.utils.StringUtil;

import utils.Tracer;

import values.other.GlobalValue;

/**
 * For Sina API 
 * @author jliang
 * 
 */
public class SinaSocialDataUtil
{
	private static var MAP:Dictionary = new Dictionary(true); 
	
	public static function getData(playerID:String, callBack:Function):void
	{
		var obj:* = MAP[playerID];
		if (!obj)
		{
			obj = { loader:null, queen: [], id:playerID  };
			MAP[playerID] = obj;
		}
		
		obj.queen.push(callBack);
		if (!obj.loader)
		{
			var loader:InfoLoader = new InfoLoader();
			loader.id = playerID;
			
			obj.loader = loader;
			
			loader.addEventListener(Event.COMPLETE, dataReadyHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			
			var request:URLRequest = new URLRequest(StringUtil.substitute(String(GlobalValue.staticData.get_social_data_url), playerID));
			request.method = URLRequestMethod.GET;
			loader.load(request);
		}
	}
	
	public static function disposeRequest(playerID:String, callBack:Function):void
	{
		var obj:* = MAP[playerID];
		if (!obj) return;
		
		if (obj.queen) 
		{
			for (var i:int = 0; i < obj.queen.length; i++)
			{
				var func:Function = obj.queen[i];
				if (func == callBack)
				{
					obj.queen.splice(i, 1);
					break;
				}
			}
		}
		
		if (obj.queen.length <= 0 && obj.loader)
		{
			var loader:InfoLoader = obj.loader;
			loader.removeEventListener(Event.COMPLETE, dataReadyHandler);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			try
			{
				loader.close();
			}
			catch (error:Error)
			{
				
			}
			
			delete MAP[playerID];
		}
	}
	
	private static function dataReadyHandler(event:Event):void
	{
		var target:InfoLoader = InfoLoader(event.target);
		target.removeEventListener(Event.COMPLETE, dataReadyHandler);
		target.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
		
		var data:Object;
		try
		{
			data = JSON.decode(target.data);
			data.first_name = data.name;
			data.last_name = "";
		}
		catch (error:Error)
		{
			Tracer.log("Can not parse player data. " + error.message);
		}
		
		var obj:* = MAP[target.id];
		for each (var callBack:Function in obj.queen)
		{			
			if (callBack != null) 
			{
				callBack(data);
			}
		}
		obj.queen = null;
		delete MAP[target.id];
	}
	
	private static function errorHandler(event:Event):void
	{
		var target:InfoLoader = InfoLoader(event.target);
		target.removeEventListener(Event.COMPLETE, dataReadyHandler);
		target.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
		
		var obj:* = MAP[target.id];
		obj.queen = null;
		delete MAP[target.id];
		
		Tracer.log("Get player data failed. " + event.toString());
	}
}
}
import flash.net.URLLoader;

class InfoLoader extends URLLoader
{
	public var id:String;
}